/**
 *  网络请求底层封装：用于定制axios库
 */

import * as axios from 'axios'
import config from '../config'

import store from '../store'
import constant from '../constant'
import router from '../router'

/**
 * 取消请求控制器：在axios的实例化中完成初始化
 * @type {null}
 */
window.axiosCancelControl = null
axios.defaults.baseURL = config.API_BASE_URL
/**
 * axios实例：用于定制请求
 */
let instance = axios.create({
  method: 'post', // 默认请求方法
  baseURL: config.API_BASE_URL,
  timeout: config.API_NORMAL_TIMEOUT,
  contentType: 'application/json;charset=UTF-8',
  responseType: 'json',

  onUploadProgress: function (progressEvent) {
    window.console.log('axios on upload progress ==========')
  },
  /**
   * 指定取消Token，能够用来取消请求
   */
  cancelToken: new axios.CancelToken(function (cancel) {
    window.axiosCancelControl = cancel
  })
})
/**
 * get请求
 *
 */
instance.getData = (url, Obj) => {
  if (Obj) {
    return axios.get(url, {
      params: Obj
    })
  } else {
    return axios.get(url)
  }
}
/**
 * 将取消请求控制器关联到axios实例中
 * @type {null}
 */
instance.cancel = function (message) {
  if (window.axiosCancelControl) {
    window.axiosCancelControl(message)
  }
}

/**
 * 请求发送之前的钩子：进行额外参数的封装
 *    应用相关信息：
 *        请求类型： ReqType: ''
 *        应用标识: AppKey： ''
 *    用户登录信息：User:{Id: '', Token: ''}
 */
instance.interceptors.request.use(reqConfig => {
  window.console.log('请求拦截器开始：==========')
  window.console.log(reqConfig)
  let regExp = /login/
  if (!regExp.test(reqConfig.url)) {
    let data = reqConfig.data
    data.userId = config.USER_ID
    data.token = config.USER_TOKEN
    try {
      store.commit(constant.GET_LOGIN_DATA)
      data.userId = store.state.auth.user.userId
      data.token = store.state.auth.user.token
      window.console.log('request interceptors ==================================')
    } catch (error) {
      window.console.log(error)
    }
  }
  return reqConfig
}, error => {
  window.console.log('请求拦截器(error)：==========')
  return Promise.reject(error)
})

/**
 * 请求处理之前的钩子：进行响应结果的处理
 */
instance.interceptors.response.use(response => {
  window.console.log('响应拦截器(normal)：==========')
  window.response = response
  // 取出服务器返回的数据
  return response.data
}, error => {
  window.console.log('响应拦截器(error)：==========')
  // 抛出错误
  return Promise.reject(error)
})

/**
 * 检查api响应是否正确
 * @param res
 * @returns {*}
 */
instance.checkResponse = function (res) {
  window.console.log('响应拦截器(checkResponse)：==========', res)

  let resultCode = parseInt(res.code)
  if (resultCode === 2000) {
    return Promise.resolve(res)
  } else {
    var error = new Error()
    error.resultCode = resultCode
    error.resultMessage = res.message
    return Promise.reject(error)
  }
}

/**
 * 请求错误处理
 * @param error
 */
instance.handleError = function (error) {
  window.console.log('错误处理，当前结果码：' + error)
  let errorMsg = JSON.stringify(error)
  let resultCode = error.resultCode || error.code || 400
  if (resultCode === 400) {
    // 网络错误
    window.$messageBox({
      message: '网络错误！！！'
    })
    window.console.log(errorMsg)
  } else {
    if (resultCode === 2016) {
      window.$messageBox({
        message: 'Token过期'
      })
      router.push({path: '/login'})
    } else if (resultCode === 10000) {
      window.$messageBox({
        message: '请求失败,错误码:' + error.resultCode + '错误信息:' + errorMsg + ''
      })
    } else {
      // 提示错误信息
      window.$messageBox({
        message: '请求失败,错误码:' + error.resultCode + '错误信息:' + errorMsg + ''
      })
    }
  }
}

export default instance
